from datetime import datetime
from typing import List, Dict, Any
import constants as con
# -------------------------- 4. 数据模型（保持与字段映射一致） --------------------------
# 集思录可转债实体类

class CbData:

    def __init__(self, data: Dict[str, Any]):
        # 1. 核心字段（与FIELD_MAPPING中的英文字段对应，补充默认值）
        self.bond_id = data.get("bond_id", "")
        self.main_update_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.bond_nm = data.get("bond_nm", "")
        self.bond_py = data.get("bond_py", "")
        self.price = data.get("price", "")
        self.increase_rt = data.get("increase_rt", "")
        self.stock_id = data.get("stock_id", "")
        self.stock_nm = data.get("stock_nm", "")
        self.stock_py = data.get("stock_py", "")
        self.sprice = data.get("sprice", "")
        self.sincrease_rt = data.get("sincrease_rt", "")
        self.pb = data.get("pb", "")
        self.convert_price = data.get("convert_price", "")
        self.convert_value = data.get("convert_value", "")
        self.convert_dt = data.get("convert_dt", "")
        self.premium_rt = data.get("premium_rt", "")
        self.dblow = data.get("dblow", "")
        self.sw_cd = data.get("sw_cd", "")
        self.market_cd = data.get("market_cd", "")
        self.btype = data.get("btype", "")
        self.list_dt = data.get("list_dt", "")
        self.t_flag = data.get("t_flag", "")
        self.bond_value = data.get("bond_value", "")
        self.rating_cd = data.get("rating_cd", "")
        self.put_convert_price = data.get("put_convert_price", "")
        self.force_redeem_price = data.get("force_redeem_price", "")
        self.convert_amt_ratio = data.get("convert_amt_ratio", "")
        self.maturity_dt = data.get("maturity_dt", "")
        self.year_left = data.get("year_left", "")
        self.curr_iss_amt = data.get("curr_iss_amt", "")
        self.volume = data.get("volume", "")
        self.svolume = data.get("svolume", "")
        self.turnover_rt = data.get("turnover_rt", "")
        self.ytm_rt = data.get("ytm_rt", "")
        self.put_ytm_rt = data.get("put_ytm_rt", "")
        self.last_time = data.get("last_time", "")
        self.qstatus = data.get("qstatus", "")
        self.adj_cnt = data.get("adj_cnt", "")
        self.adj_scnt = data.get("adj_scnt", "")
        self.convert_price_tips = data.get("convert_price_tips", "")
        self.convert_cd_tip = data.get("convert_cd_tip", "")
        self.ref_yield_info = data.get("ref_yield_info", "")
        self.orig_iss_amt = data.get("orig_iss_amt", "")
        self.price_tips = data.get("price_tips", "")
        self.redeem_dt = data.get("redeem_dt", "")
        self.real_force_redeem_price = data.get("real_force_redeem_price", "")
        self.after_next_put_dt = data.get("after_next_put_dt", "")
        self.icons = data.get("icons", "")
        self.is_min_price = data.get("is_min_price", "")
        self.blocked = data.get("blocked", "")
        self.putting = data.get("putting", "")
        self.owned = data.get("owned", "")
        self.hold = data.get("hold", "")
        self.option_value = data.get("option_value", "")
        self.fund_rt = data.get("fund_rt", "")
        self.noted = data.get("noted", "")
        self.sqflag = data.get("sqflag", "")
        self.pb_flag = data.get("pb_flag", "")
        self.convert_price_valid = data.get("convert_price_valid", "")
        self.adjusted = data.get("adjusted", "")
        self.option_tip = data.get("option_tip", "")
        self.force_redeem_price_tip = data.get("force_redeem_price_tip", "")
        self.notes = data.get("notes", "")
        self.expire_rt = data.get("expire_rt", "")

        # 数据更新时间（标准化格式）

        # 2. 动态补充其他字段（若集思录返回更多字段，自动添加）
        for key, value in data.items():
            # 当前对象 self 本身没有名为 key 的属性
            if key not in con.FIELD_MAPPING and not hasattr(self, key):
                # setattr 函数给 self 对象动态添加一个属性，属性名字是key,值是value或者空字符串
                setattr(self, key, value if value is not None else "")

